//
//  YXStatusCell.swift
//  微博YX
//
//  Created by apple on 15/11/27.
//  Copyright © 2015年 lyx. All rights reserved.
//

import UIKit

class YXStatusCell: UITableViewCell {
    
    //    顶部的view
    private  lazy var statusTopView: YXStatusTopView = YXStatusTopView()
    //    底部的view
    lazy var statusBottonView: YXStatusBottonView = YXStatusBottonView()
    //    配图
    lazy var pictureView: YXPictureView = YXPictureView()
//    视图的宽高
    var pictureViewWidth: NSLayoutConstraint?
    var pictureViewHight: NSLayoutConstraint?
    //    微博内容的文本
    /// 微博文本内容
     lazy var contentLabel: UILabel = {
        let label = UILabel(color: UIColor.blackColor(), fonSize: 14)
        
        // 设置换行
        label.numberOfLines = 0
        
        return label
        }()
    
    //    微博模型
    var status: YXStatus? {
        didSet{
            statusTopView.status = status
            contentLabel.text = status?.text
//            print("status的图片url：\(status?.pictureURLs)")
            pictureView.status = status
//            设置配图的高度
            let size = pictureView.calcViewSize()
//            print("size:\(size)")
//            设置宽高
            pictureViewWidth?.constant = size.width
//            print("pictureViewWidth?.constant：\(pictureViewWidth?.constant)")
            pictureViewHight?.constant = size.height
//            print("pictureViewHight?.constant：\(pictureViewHight)")

//            // 测试改变配图视图高度
//            let count = arc4random_uniform(4)
//            let height = count * 90
//            pictureViewHight?.constant = CGFloat(height)
         }
    }
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
//        self.contentView.backgroundColor = UIColor.redColor()
        prepareUI()
    }
//    计算行高
    func rowHight(status: YXStatus) -> CGFloat {
        self.status = status
//        根据新的内容更新约束
        self.layoutIfNeeded()
//        print("计算出来的行高：\(CGRectGetMaxY(statusBottonView.frame))")
        return CGRectGetMaxY(statusBottonView.frame)
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    func prepareUI(){
        contentView.addSubview(statusTopView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(statusBottonView)
        
        //        头部的view
        statusTopView.ff_AlignInner(type: ff_AlignType.TopLeft, referView: contentView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 53))
        //        微博内容的label
        contentLabel.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: statusTopView, size: nil, offset: CGPoint(x: 8, y: 8))
        //        宽度
        contentView.addConstraint(NSLayoutConstraint(item: contentLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: UIScreen.mainScreen().bounds.width - 2 * 8))
//        //        配图的view
//       let cons = pictureView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width: 290, height: 290), offset: CGPoint(x: 0, y: 8))
//        pictureViewWidth = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Width)
//        pictureViewHight = pictureView.ff_Constraint(cons, attribute: NSLayoutAttribute.Height)
        //        底部的view
        statusBottonView.ff_AlignVertical(type: ff_AlignType.BottomLeft, referView: pictureView, size: CGSize(width: UIScreen.mainScreen().bounds.width, height: 44), offset: CGPoint(x: 0, y: 8))
////        约束cell的高度和底部按钮重合
//              contentView.addConstraint(NSLayoutConstraint(item: contentView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: statusBottonView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
    }

    
    
    

}
